package blog.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.blog.logic.UserLogic;
import org.validation.UserValidator;

import blog.controller.UserController;

public class User extends ActionForm{

	private int id;
	private String login = "";
	private String password = "";
	private String mail = "";
	private String name = "";
	private String surname = "";
	private String description = "";
	private String type;
	private String style;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getStyle() {
		return style;
	}
	public void setStyle(String style) {
		this.style = style;
	}
	
	@Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

        ActionErrors errs = new ActionErrors();
        
        if(UserLogic.getInstance().getUser(login) != null)
        	if(!UserController.isLogged(request.getSession()))
        		errs.add("noUser", new ActionMessage("errors.loginZajety"));
        
        if(!UserValidator.isPasswordValid(password))
        	errs.add("userE", new ActionMessage("errors.bledneHaslo"));
        
        if(!UserValidator.isLoginValid(login))
        	errs.add("passE", new ActionMessage("errors.blednyLogin"));
        
        if(!UserValidator.isMailValid(mail))
        	errs.add("mailE", new ActionMessage("errors.blednyMail"));
        
        if(errs.isEmpty())
            return null;
        else
            return errs;
    }
	
}
